//
//  BPViewController.m
//  BPScreenManager
//
//  Created by xuhongji on 04/29/2020.
//  Copyright (c) 2020 xuhongji. All rights reserved.
//

#import "BPViewController.h"
#import <BPScreenManager/BPScreenManager.h>
#import <BPScreenManager/BPScreenFileManager.h>
#import <BPRequestTool.h>
@interface BPViewController ()<BPScreenShotDelegate>

@end

@implementation BPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"test for screent blur";
    [self.view addSubview:label];
    [BPScreenManager shareInstance].shoter.cache = NO;
    [BPScreenManager shareInstance].shoter.delegate = self;
}

- (void)screenShot:(BPScreenShotManager *)manager didShotWithScreenImage:(UIImage *)image{
    
}


- (void)test {
    NSArray *filePaths = [BPScreenFileManager allFilePathArrayWithSplitSize:5];
    NSMutableArray *allPaths = [NSMutableArray arrayWithArray:filePaths];
    while (allPaths.count > 0) { //5个批量上传一次
        [self uploadDataWithFiles:allPaths.lastObject];
        [allPaths removeLastObject];
    }
}

//模拟文件上传
-(void)uploadDataWithFiles:(NSArray*)files{
    
    NSArray *fileSource = [files copy];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = @"test";
    
    [BPRequestTool POST:@"URL_UploadScreenShot" parameters:params requestSerializer:BPRequestSerializerJson responseSerializer:BPResponseSerializerTypeJson constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nullable formData) {
        for (NSString*filePath in fileSource) {
            NSString *fileName = filePath.lastPathComponent;
            NSData *data = [NSData dataWithContentsOfFile:filePath];
            [formData appendPartWithFileData:data name:@"screen_shot" fileName:fileName mimeType:@"image/jpeg"];
        }
    } progress:nil success:^(id  _Nullable responseObject) {
        for (NSString*filePath in fileSource) {//移除文件夹中图片
            [BPScreenFileManager deleteFileWithPath:filePath];
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
